<?php
class Admin_AreaDestinationController extends Apps_Controller_Action_Admin {
	//LIST
	public function indexAction() {
		$this->actResolve('act_delete',
				'delete', null, null, array('id' => $this->_getParam('item_checker')),
				true);

		$tbAreaDestination = new M_DbTable_AreaDestination();
		$grid = new Apps_Plugin_Grid('AreaGrid');

		$grid->setData($tbAreaDestination->getList())
			 ->setTableCaption('List Area Destination')
			 ->setTableColumnFormat(array(
			 		array('title'		=> 'ID',
			 			  'dataField'	=> 'id',
			 			  'width'		=> '5%',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Country',
			 			  'dataField'	=> 'country_name',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Area Destination',
			 			  'dataField'	=> 'en_name',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Area Destination (Vi)',
			 			  'dataField'	=> 'name',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Description',
			 			  'dataField'	=> 'description',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Created Date',
			 			  'dataField'	=> 'created_date',
			 			  'class'		=> 'center'),
			 		array('title'      	=> 'Edit',
			 			  'html'       	=> array('<a href="/admin/area-destination/edit?id=%s"><i class="icon-edit" title="Sửa Danh Mục"></i></a>',
			 			  				  array('id')),
			 			  'width'      	=> '2%')
			 		));
		$this->view->grid = $grid;
	}
	//ADD
	public function addAction() {
		$rq = $this->_request;

		$tbAreaDestination = new M_DbTable_AreaDestination();
		$f 	= new Admin_Form_AddAreaDestination();

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbAreaDestination->insert($f->getValues())
					? self::$msgAddSuccess
					: self::$msgAddFailed);

				$this->_helper->redirector('index', null, null);
			}
		}
		$this->view->form = $f;
	}

	//EDIT
	public function editAction() {
		$rq = $this->_request;
		$id = $rq->getParam('id');

		$tbAreaDestination = new M_DbTable_AreaDestination();

		if(!$id || !is_numeric($id) || !$tbAreaDestination->fetchRow(array('id =?' => $id))) {
			$this->_helper->redirector('index', null, null);
		}

		$f 	= new Admin_Form_AddAreaDestination();
		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbAreaDestination->update($f->getValues(), array('id =?' => $id))
					? self::$msgUpdateSuccess
					: self::$msgUpdateFailed);
				$this->_helper->redirector('index', null, null);
			}
		}

		$f->setLegend('Edit AreaDestination');
		$f->populate($tbAreaDestination->fetchRow(array('id =?' => $id))->toArray());
		$this->view->form = $f;
		$this->render('add');
	}

	//DELETE
	public function deleteAction() {
		$this->disableLayout(true, true);

		$id = $this->_getParam('id');
		if (!$id) {
			$this->_helper->redirector('index', null, null);
		}

		$tbAreaDestination = new M_DbTable_AreaDestination(); 
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (is_numeric($v)) {
					$tbAreaDestination->update(array('status' => $tbAreaDestination::STATUS_DELETED),
											array('id =?'  => $v));
				}
			}
		} else if (is_numeric($id)) {
				if ($tbAreaDestination->update(array('status' => $tbAreaDestination::STATUS_DELETED),
										array('id =?'  => $id)))
				{
					$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
				}
		}
		$this->_helper->redirector('index', null, null);
	}
}